package vue;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.BadLocationException;
import javax.swing.undo.UndoManager;

public class ControleurEditeur implements DocumentListener, UndoableEditListener, MouseListener
{
	private Editeur m_editeur = null;
	private ControleurArticle m_controleurArticle;
	private UndoManager undoManager;	//gestion undo/redo
	private PopupSuggestions _popup;
	private boolean _remplacement;
	
	public ControleurEditeur (ControleurArticle ca)
	{
		m_controleurArticle = ca;
		build ();
	}
	
	private void build()
	{
		this.undoManager = new UndoManager();
		this.m_editeur = new Editeur (this);
		_popup = new PopupSuggestions(this);
		_remplacement = false;
	}
	
	public ControleurArticle getControleurArticle()
	{
		return m_controleurArticle;
	}
	
	public UndoManager getUndoManager()
	{
		return this.undoManager;
	}

	public Editeur getView()
	{
		return this.m_editeur;
	}
	
	@Override
	public void undoableEditHappened(UndoableEditEvent e)
	{
		this.undoManager.addEdit(e.getEdit());
	}
	
	//Retablir une action
	public void retablir()
	{
		if (this.undoManager.canRedo())
			this.undoManager.redo();
	}
	
	//Annuler une action
	public void annuler()
	{
		if (this.undoManager.canUndo())
			this.undoManager.undo();
	}
	
	@Override
	public void changedUpdate(DocumentEvent event) 
	{
		// TODO Auto-generated method stub
		System.out.println("changed");
	}
	
	public void remplacerMot(String nouveauMot, int position)
	{
		int[] pos;
		if ((pos = m_controleurArticle.remplacerMot(nouveauMot, position)) != null)
		{
			_remplacement = true;
			m_editeur.remplacerMot(nouveauMot, pos[0], pos[1]);
			m_controleurArticle.lancerAnanlyseMotParMot();
		}
		_remplacement = false;
	}
	
	@Override
	public void insertUpdate(DocumentEvent event) 
	{
		if (!_remplacement)
		{
			try
			{
				m_controleurArticle.ajouter(m_editeur.getDocument().getText(event.getOffset(), event.getLength()), event.getOffset());
			}
			catch (BadLocationException e)
			{
				e.printStackTrace();
			}
		}
	}

	@Override
	public void removeUpdate(DocumentEvent event) 
	{
		if (!_remplacement)
		{
			m_controleurArticle.supprimer(event.getOffset(), event.getOffset() + event.getLength());
		}
	}
	

	@Override
	public void mouseClicked(MouseEvent e) {
	}

	@Override
	public void mouseEntered(MouseEvent e)	{
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}

	@Override
	public void mousePressed(MouseEvent e)
	{
		if (e.isPopupTrigger())
		{
			int pos = m_editeur.getCaret().getDot();
			List<String> list = m_controleurArticle.getSuggestions(pos);
			if (list != null)
			{
				_popup.setSuggestions(list, pos);
				_popup.updateSuggestions();
				_popup.show(e.getComponent(), e.getX(), e.getY());
			}
		}
	}

	@Override
	public void mouseReleased(MouseEvent e)
	{
		if (e.isPopupTrigger())
		{
			int pos = m_editeur.getCaret().getDot();
			List<String> list = m_controleurArticle.getSuggestions(pos);
			if (list != null)
			{
				_popup.setSuggestions(list, pos);
				_popup.updateSuggestions();
				_popup.show(e.getComponent(), e.getX(), e.getY());
			}
		}
	}
}
